/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import com.mindprod.entities.StripEntities;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * A useful Class to deal with JSONObjects requests.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class JSONUtils {
  
  /**
   * Gets the int value of a JSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @param defaultValue the default value on error
   * @return the jSON int
   */
  public static int getJSONInt(JSONObject jsonObject, String key, 
      int defaultValue) {
    try {
      return jsonObject.getInt(key);
    } catch (JSONException e) {
      return defaultValue;
    }
  }

  /**
   * Gets the boolean value of a jSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @param defaultValue the default value on error
   * @return the jSON boolean on error
   */
  public static boolean getJSONBoolean(JSONObject jsonObject, String key, 
      boolean defaultValue) {
    try {
      return jsonObject.getBoolean(key);
    } catch (JSONException e) {
      return defaultValue;
    }
  }

  /**
   * Gets the long value of a jSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @param defaultValue the default value on error
   * @return the jSON long
   */
  public static long getJSONLong(JSONObject jsonObject, String key, 
      long defaultValue) {
    try {
      return jsonObject.getLong(key);
    } catch (JSONException e) {
      return defaultValue;
    }
  }

  /**
   * Gets the double value of a jSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @param defaultValue the default value on error
   * @return the jSON double
   */
  public static double getJSONDouble(JSONObject jsonObject, String key, 
      double defaultValue) {
    try {
      return jsonObject.getDouble(key);
    } catch (JSONException e) {
      return defaultValue;
    }
  }

  /**
   * Gets the string value of a jSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @param defaultValue the default value on error
   * @return the jSON string
   */
  public static String getJSONString(JSONObject jsonObject, String key, 
      String defaultValue) {
    try {
      String value = jsonObject.getString(key);
      return StripEntities.stripHTMLEntities(value, ' ');
    } catch (JSONException e) {
      return defaultValue;
    }
  }

  /**
   * Gets the jsonobject value of a jSON object
   *
   * @param jsonObject the json object
   * @param key the key
   * @return the jSON object
   */
  public static JSONObject getJSONObject(JSONObject jsonObject, String key) {
    try {
      return jsonObject.getJSONObject(key);
    } catch (JSONException e) {
      return null;
    }
  }
  
  /**
   * Makes sure that the JSON object returned will be an JSONArray
   *
   * @param jsonObject the json object
   * @param parent the parent
   * @param key the key
   * @return the jSON array
   */
  public static JSONArray getJSONArray(JSONObject jsonObject, String parent, 
      String key) {
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonParent;
    try {
      jsonParent = jsonObject.getJSONObject(parent);      
    } catch (JSONException e) {
      return jsonArray;
    } 
    try {
      return jsonParent.getJSONArray(key);
    } catch (JSONException e) {
      try {
        jsonArray.put(jsonParent.getJSONObject(key));
      } catch (JSONException e2) {
        try {
          jsonArray.put(jsonParent.getString(key));
        } catch (JSONException e1) {
          DumpExceptions.dumpJSONException(e2);
        }
      }
      return jsonArray;
    }  
  }

  /**
   * Makes sure that the JSON object returned will be an JSONArray
   *
   * @param jsonObject the json object
   * @param key the key
   * @return the jSON array
   */
  public static JSONArray getJSONArray(JSONObject jsonObject, String key) {
    JSONArray jsonArray = new JSONArray();
    try {
      // first, lets try to get the key as an array
      return jsonObject.getJSONArray(key);
    } catch (JSONException e) {
      try {
        // so, lets try to get the key as an JSONObject
        jsonArray.put(jsonObject.getJSONObject(key));
      } catch (JSONException e1) {
        // finally, lets try to get the key as a String
        String value;
        try {
          value = jsonObject.getString(key);
          jsonArray.put(value);
        } catch (JSONException e2) {
          DumpExceptions.dumpJSONException(e2);
        }        
      }
      return jsonArray;
    }  
  }
}